class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self):
        self.root = None

    def insert(self, value, target=None, direction=None):
        if self.root is None:
            self.root = Node(value)
        else:
            if target is None or direction is None:
                raise ValueError("Both target and direction must be specified for custom insertion.")
            target_node = self._find_node(target, self.root)
            if target_node is None:
                raise ValueError("Target node not found.")
            if direction == "left":
                if target_node.left is None:
                    target_node.left = Node(value)
                else:
                    raise ValueError("Target node already has a left child.")
            elif direction == "right":
                if target_node.right is None:
                    target_node.right = Node(value)
                else:
                    raise ValueError("Target node already has a right child.")
            else:
                raise ValueError("Direction must be either 'left' or 'right'.")

    def _find_node(self, value, node):
        if node is None or node.value == value:
            return node
        left_result = self._find_node(value, node.left)
        right_result = self._find_node(value, node.right)
        return left_result if left_result else right_result

    def search(self, value):
        return self._search_recursive(value, self.root)

    def _search_recursive(self, value, node):
        if node is None or node.value == value:
            return node
        left_result = self._search_recursive(value, node.left)
        right_result = self._search_recursive(value, node.right)
        return left_result if left_result else right_result

    def delete(self, value):
        self.root = self._delete_recursive(value, self.root)

    def _delete_recursive(self, value, node):
        if node is None:
            return node
        if node.value == value:
            if node.left is None:
                return node.right
            elif node.right is None:
                return node.left
            else:
                successor = self._get_min_value_node(node.right)
                node.value = successor.value
                node.right = self._delete_recursive(successor.value, node.right)
        else:
            node.left = self._delete_recursive(value, node.left)
            node.right = self._delete_recursive(value, node.right)
        return node
    
    def _get_min_value_node(self, node):
        current = node
        while current.left is not None:
            current = current.left
        return current

    def inorder_traversal(self):
        result = []
        self._inorder_traversal_recursive(self.root, result)
        return result

    def _inorder_traversal_recursive(self, node, result):
        if node is not None:
            self._inorder_traversal_recursive(node.left, result)
            result.append(node.value)
            self._inorder_traversal_recursive(node.right, result)


# 使用示例:
tree = BinaryTree()
tree.insert(5)  # 插入根节点

tree.insert(3, target=5, direction="left")  # 在值为5的节点左侧插入值为3的节点
tree.insert(2, target=5, direction="right")  # 在值为5的节点右侧插入值为2的节点

tree.insert(4, target=3, direction="left")  # 在值为3的节点左侧插入值为4的节点
tree.insert(6, target=3, direction="right")  # 在值为3的节点右侧插入值为6的节点

print(tree.search(4))  # 输出：Node object with value 4
print(tree.search(8))  # 输出：None

tree.delete(3)
print(tree.inorder_traversal())  # 输出：[2, 4, 5, 6]